package com.ljz.controller;

import com.ljz.dao.OrdersRepository;
import com.ljz.entity.Orders;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jiazhang on 2017/4/12.
 */

@RestController

public class OrdersController {

    @Autowired
    OrdersRepository ordersRepository;

    /**
     * 查询所有信息
     * @return
     */
    @GetMapping(value = "/orders")
    private List<Orders> ordersList() {
        return ordersRepository.findAll();
    }

    /**
     * add data
     * @param oid
     * @param odatetime
     * @param otate
     * @param osummoney
     * @param cid
     * @return
     */
    @PostMapping(value = "/orders")
    public Orders ordersAdd(@RequestParam("oid") Integer oid,
                            @RequestParam("odatetime") String odatetime,
                            @RequestParam("ostate") String otate,
                            @RequestParam("osummoney") Double osummoney,
                            @RequestParam("cid") Integer cid) {
        Date date = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(odatetime);
        } catch (ParseException s) {

        }

        Orders orders = new Orders();
        orders.setCid(cid);
        orders.setOdatetime(date);
        orders.setOid(oid);
        orders.setOstate(otate);
        orders.setOsummoney(osummoney);

        return ordersRepository.save(orders);
    }
}
